#include <my_header.h>

/* Usage:  ./19_rwlock*/
int num = 10;
// 定义锁 当读锁或写锁都可以
pthread_rwlock_t rLock = PTHREAD_RWLOCK_INITIALIZER;
void *func(void *arg)
{
    // 读锁
    pthread_rwlock_rdlock(&rLock);
    printf("I am child thread %ld, num = %d\n",
           pthread_self(), num);
    sleep(5);
    printf("%ld free lock \n", pthread_self());
    pthread_rwlock_unlock(&rLock);
    return NULL;
}

int main(void)
{
    // 创建三个子线程全部都指向入口函数func:wq
   pthread_t pid1, pid2, pid3;
   pthread_create(&pid1, NULL, func, NULL);
   pthread_create(&pid2, NULL, func, NULL);
   pthread_create(&pid3, NULL, func, NULL);

   // 捕获三个子线程 阻塞并清理资源
   pthread_join(pid1, NULL);
   pthread_join(pid2, NULL);
   pthread_join(pid3, NULL);

   // 销毁锁
   pthread_rwlock_destroy(&rLock);
   return 0;
}

